package com.audit.controller;

import com.audit.service.AuditService;
import com.audit.service.CityService;
import com.common.constant.enums.CommonStatus;
import com.common.entity.TbCity;
import com.common.entity.audit.TbAudit;
import com.common.response.CommonResponse;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 蛋儿
 * @date 2021/8/11 19:43
 *
 * 审核管理
 */

@RestController
@RequestMapping("/audit")
@CrossOrigin
public class AuditController {

    @Autowired
    private AuditService auditService;

    @Autowired
    private CityService cityService;


    /**
     * 审核管理列表
     * @param audit
     * @param page
     * @param size
     * @return
     */
    @PostMapping("/findPage")
    public PageInfo<TbAudit> findPage(
            @RequestBody TbAudit audit,
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "size",defaultValue = "5") Integer size
    ){
        return auditService.findAll(page,size,audit);
    }


    /**
     * 审核删除
     * @param id
     * @return
     */
    @DeleteMapping("delete/{id}")
    public CommonResponse delete(@PathVariable("id")Long id){
        try {
            auditService.delete(id);
            return new CommonResponse(CommonStatus.SUCCESS.getCode(),CommonStatus.SUCCESS.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return new CommonResponse(CommonStatus.ERROR.getCode(),CommonStatus.ERROR.getMessage());
        }
    }


    /**
     * 查询单条数据
     * @param id
     * @return
     */
    @GetMapping("/findOne")
    public TbAudit findOne(Long id){
        return auditService.findOne(id);
    }

    /**
     * 修改
     * @param audit
     * @return
     */
    @PutMapping("/update")
    public CommonResponse delete(@RequestBody TbAudit audit){
        try {
            auditService.update(audit);
            return new CommonResponse(CommonStatus.SUCCESS.getCode(),CommonStatus.SUCCESS.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return new CommonResponse(CommonStatus.ERROR.getCode(),CommonStatus.ERROR.getMessage());
        }
    }


    /**
     * 审核通过或不通过状态
     * @param id
     * @param status
     */
    @PutMapping("/updateStatus")
    public void updateStatus(Long id ,Integer status){
        auditService.updateStatus(id,status);
    }


    /**
     * 三级联动
     * @param pid
     * @return
     */
    @GetMapping("/findCity")
    public List<TbCity> findCity(Integer pid){
        return cityService.findCity(pid);
    }


}
